<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>咨询栏目</title>
    <!--引入vue.js库-->
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> -->
    <script src="/js/vue.js"></script>
    <!-- element-ui引入样式 -->
    <link rel="stylesheet" href="/css/index.css">
    <!-- element-ui引入组件库 -->
    <script src="/js/index.js"></script>
    <!--引入axios库-->
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script>-->
    <script src="/js/axios.min.js"></script>
</head>
<body>
<div id="app">
    <!-- 根据参数查询 和 添加数据 -->
    <el-row>
        <el-col :span="4">
            <el-input v-model="searchForm.name" placeholder="请输入查询名"></el-input>
        </el-col>
        <el-col :span="3">&nbsp;</el-col>
        <el-col :span="8">
            <el-button type="success" size="mini" icon="el-icon-search" @click="search">搜索</el-button>
            <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAdd">添加</el-button>
            <el-button type="primary" size="mini" icon="el-icon-refresh" @click="refresh">刷新</el-button>
        </el-col>
        <el-col :span="6"></el-col>
    </el-row>

    <!--    查询-->
    <el-table :data="tableData" border style="width: 100%">
<!--        <el-table-column width="210" prop="code" label="咨询代码"></el-table-column>-->
        <el-table-column width="200" prop="name" label="咨询分类名"></el-table-column>
        <el-table-column width="120" prop="isedit" :formatter="getStatus1" label="是否允许修改"></el-table-column>
        <el-table-column width="120" :formatter="getStatus" prop="isvalid" label="是否有效"></el-table-column>
<!--        <el-table-column width="120" prop="sortno" label="排序号"></el-table-column>-->
        <el-table-column width="120" :formatter="getStatus2" prop="isweb" label="是否作为web栏目"></el-table-column>
        <el-table-column width="210" prop="description" label="描绘"></el-table-column>
        <!-- 操作设置 -->
        <el-table-column fixed="right" width="300" label="操作" width="140">
            <template slot-scope="scope">
                <el-button
                        type="primary"
                        @click="handleEdit(scope.$index, scope.row)">编辑
                </el-button>
                <el-button type="danger" @click="deleteDept(scope.row.code)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页功能 -->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[6,12,120]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
    <!--    添加修改弹窗-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" ref="form">
            <el-form-item label="咨询代码" :label-width="formLabelWidth">
                <el-input v-model="form.code" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="咨询分类名" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="修改" :label-width="formLabelWidth">
                <el-select v-model="form.isedit" placeholder="请选是否允许">
                    <el-option label="不允许" value="0"></el-option>
                    <el-option label="允许" value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否有效" :label-width="formLabelWidth">
                <el-select v-model="form.isvalid" placeholder="请选是否有效">
                    <el-option label="有效" value="1"></el-option>
                    <el-option label="无效" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="排序号" :label-width="formLabelWidth">
                <el-input v-model="form.sortno" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="web栏目" :label-width="formLabelWidth">
                <el-select v-model="form.isweb" placeholder="请选是否作为">
                    <el-option label="不作为" value="0"></el-option>
                    <el-option label="作为" value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="描绘" :label-width="formLabelWidth">
                <el-input v-model="form.description" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        </div>
    </el-dialog>

</div>
<script>
    var vue = new Vue({
        el: '#app',
        data() {
            return {
                //表格数据
                tableData: [],//返回的查询数据
                currentPage: 1,//页码
                pageSize: 6,//每页显示的数量
                total: 100,//分页总数量
                searchForm: {name: ''},//查询携带的参数
                dialogFormVisible: false, //使弹出不显示
                title: '景区分类添加', //title 默认为 攻略添加
                //添加或者更新数据时 携带的参数
                form: {code: '',
                    name: '',
                    isedit:'',
                    isvalid:'',
                    sortno: '',
                    isweb:'',
                    description: ''},//添加或者修改时携带的参数
                //弹出框的宽度
                formLabelWidth: '120px',
                rules: {
                    code: [
                        {required: true, message: '请输入攻略代码', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '请输入攻略名称', trigger: 'blur'}
                    ],
                    isedit: [
                        {required: true, message: '请选择是否允许修改', trigger: 'blur'}
                    ],
                    isvalid: [
                        {required: true, message: '请选择是否有效', trigger: 'blur'}
                    ],
                    isweb: [
                        {required: true, message: '请选择是否作为web栏目', trigger: 'blur'}
                    ],
                }
            }
        },
        //加载时默认执行的方法
        created: function () {
            //调用封装的方法
            this.loadData();
        },
        methods: {//定义变量_this等于外面的this
            //封装请求数据的方法
            loadData() {
                var _this = this;
                axios.post('/nechannl/queryAll', {
                    pageNo: this.currentPage,
                    pageSize: this.pageSize,
                    data: {
                        "name": this.searchForm.name
                    }
                })
                    .then(function (response) {
                        console.log(response);
                        //判断是否执行成功
                        if (response.data.code == 200) {
                            _this.tableData = response.data.data.list;
                            _this.total = response.data.data.total;
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            getStatus(row, column, cellValue, index) {
                return cellValue == 1 ? "有效" : "无效";
            },
            getStatus1(row, column, cellValue, index) {
                return cellValue == 1 ? "允许" : "不允许";
            },
            getStatus2(row, column, cellValue, index) {
                return cellValue == 1 ? "作为" : "不作为";
            },
            //实行每页有多少条的方法
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.loadData();
            },
            //实现跳转页的方法
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.loadData();
            },
            //刷新
            refresh() {
                //原始js重新请求
                location.reload();
            },
            //查询方法
            search() {
                this.loadData();
                //搜索时,使页码为1
                this.currentPage = 1;
            },
            //攻略更新的方法
            handleEdit(index, row) {
                console.log(index, row);
                //使弹出框显示
                this.dialogFormVisible = true;
                //给用户赋值
                this.title = '景区攻略更新';
                //点击查询携带数据
                this.form = row;
            },
            //添加数据方法
            handleAdd() {
                //使弹出框显示
                this.dialogFormVisible = true;
                //给用户赋值
                this.title = '景区攻略添加';
                //点击查询携带数据
                this.form = {}
            },
            //关闭方法
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {
                    });
            },
            //删除方法
            deleteDept(code) {
                this.$confirm('此操作将删除该攻略, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }).then(() => {
                    var _this = this;
                    axios.delete("/nechannl/delete/" + code)
                        .then(function (response) {
                            console.log(response);
                            if (response.data.code == 200) {
                                _this.$message({
                                    message: '操作成功',
                                    type: 'success'
                                });
                                _this.dialogFormVisible = false;
                                _this.loadData();
                            } else {
                                _this.$message("操作失败")
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                })
            },
            //实现用户添加或者更新
            addOrUpdate() {
                var uri = "/nechannl/add";
                if (this.title == '景区攻略更新') {
                    uri = "/nechannl/update";
                }
                var _this = this;
                axios.post(uri, this.form)
                    .then(function (response) {
                        console.log(response);
                        if (response.data.code == 200) {
                            _this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                            _this.dialogFormVisible = false;
                            _this.loadData();
                        } else {
                            _this.$message("操作失败")
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            }
        }
    })
</script>
</body>
</html>